﻿Partial Public Class FolderDialog

    Private Sub FolderDialog_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives
            Dim Item As New TreeViewItem
            Item.Header = Drive.Name
            Item.Tag = Drive.Name
            AddHandler Item.Expanded, AddressOf treeExplorer_Expanded
            TreeView1.Items.Add(Item)
        Next
    End Sub

    Private Sub treeExplorer_Expanded(ByVal sender As Object, ByVal e As RoutedEventArgs)

        Dim ExpandedItem As TreeViewItem = DirectCast(sender, TreeViewItem)
        Dim Path As String = DirectCast(ExpandedItem.Tag, String)

        Debug.WriteLine(Path)

        ExpandedItem.Items.Clear() ' i think this causes the expanded event of the root node to be raised 

        For Each Folder As String In IO.Directory.GetDirectories(Path)
            Dim Item As New TreeViewItem
            Item.Header = Folder.Substring(Folder.LastIndexOf("\") + 1)
            Item.Tag = Folder
            AddHandler Item.Expanded, AddressOf treeExplorer_Expanded
            ExpandedItem.Items.Add(Item)
        Next

    End Sub
End Class
